﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

using WinFormsUI.Docking;

using ChaSQLite.SQLiteManage;

namespace ChaSQLite.SubForm
{
    public partial class QueryViewer : DockContent
    {
        private string title = "QueryEditor";

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public QueryViewer()
        {
            InitializeComponent();
        }

        private void QueryViewer_Load(object sender, EventArgs e)
        {
            this.mainSplitter.Panel2Collapsed = true;
            this.Text = title;
            this.TabText = title;
        }

        private bool doQuery(string query)
        {
            query = query.Trim();
            char [] value = new char[] { ' ', '\t' };
            int nIndex = query.IndexOfAny(value);
            string cmd = null;

            if (nIndex > 0)
            {
                cmd = query.Substring(0, nIndex);
                if (cmd.ToUpper().Equals("SELECT") == false)
                    cmd = null;
            }

            if (cmd == null)
            {
                SQLManager.getInstance.GetBase.ExecuteNonQuery(query);
                return true;
            }

            //try 
            {
                DataTable table = SQLManager.getInstance.GetBase.ExecuteQuery(query);
                
                if (table.Columns.Count == 0)
                    return false;

                int nColumns = table.Columns.Count;
                string[] colName = new string[nColumns];
                int nCount = 0;

                lstResult.Clear();
                lstResult.Columns.Clear();                

                foreach (DataColumn col in table.Columns)
                {
                    colName[nCount++] = col.ToString();
                    lstResult.Columns.Add(col.ToString());
                }

                int nFirst = 0;
                //ListViewItem item;
                bool r = false;
                foreach (DataRow row in table.Rows)
                {
                    nFirst = 0;
                    foreach (DataColumn column in table.Columns)
                    {
                        colName[nFirst++] = row[column].ToString();
                    }

                    ListViewItem newItem = new ListViewItem( colName, 0 ); //info.Message }, imageindex);
                    if (r == true)
                        newItem.BackColor = Color.FromName("WhiteSmoke");
                    
                    lstResult.Items.Add(newItem);
                    r = r == false ? true : false;
                }

                this.mainSplitter.Panel2Collapsed = false;
            }
            //catch(Exception e)
            {
            //    MessageBox.Show( e.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }

            return true;
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            List<string> querys = getQueryStrings(txtQueryString.Text);

            foreach ( string query in querys )
            {
                doQuery( query );
            }            
        }

        private List<string> getQueryStrings(string query)
        {
            List<string> arr = new List<string>();
            int nCount = query.Length;
            string queryText = "";

            for (int i = 0; i < nCount; i++)
            {
                if (query[i] == ';')
                {
                    arr.Add(queryText);
                    queryText = "";
                }
                else if (query[i] == '\n' || query[i] == '\r')
                    continue;
                else
                {
                    queryText += query[i];
                }
            }
            arr.Add(queryText);
            
            return arr;
        }
    }
}
